package com.sskj.gocoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.widget.ToolBarLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BannerWebActivity extends AppCompatActivity {

    @BindView(R2.id.banner_web)
    WebView bannerWeb;
    @BindView(R2.id.asset_toolbar)
    ToolBarLayout assetToolbar;
    @BindView(R2.id.llWeb)
    LinearLayout llWeb;

    private AgentWeb mAgentWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_web);
        ButterKnife.bind(this);
        assetToolbar.setLeftButtonOnClickLinster(v -> finish());
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url)) {
            if (url.contains("http")) {
                initView(url);
            } else {
                initView("http://" + url);
            }
        }
    }

    public static void start(Context context, String url) {
        Intent intent = new Intent(context, BannerWebActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

//    public void initView(String url) {
//        //覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为，使得网页可以在WebView中打开
//        bannerWeb.loadUrl(url);
//        WebSettings webSettings = bannerWeb.getSettings();
//        webSettings.setJavaScriptEnabled(true);//允许使用js
//        webSettings.setDisplayZoomControls(false);
//        webSettings.setDomStorageEnabled(true);
//        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据.
//        //支持屏幕缩放
//        webSettings.setSupportZoom(true);
//        webSettings.setBuiltInZoomControls(true);
//        bannerWeb.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
////                progress.dismiss();
//            }
////            @Override
////            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
////                view.loadUrl(url);
////                return super.shouldOverrideUrlLoading(view, request);
////            }
//        });
//    }

    public void initView(String url) {
        ((ViewGroup) (bannerWeb.getParent())).removeView(bannerWeb);
        bannerWeb.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(llWeb, new LinearLayout.LayoutParams(-1, -1))
//                .useDefaultIndicator(Color.TRANSPARENT)// 使用默认进度条
                .useDefaultIndicator()// 使用默认进度条
                .setWebView(bannerWeb)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //注意这里开启 strict 模式 ， 设备低于 4.2 情况下回把注入的 Js 全部清空掉 ， 这里推荐使用 onJsPrompt 通信
                .createAgentWeb()
                .ready()
                .go(url);
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.i("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
//
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) webView.getLayoutParams();
//            params.width = ScreenUtils.getScreenWidth(WebViewActivity.this);
//            params.height = ScreenUtils.getScreenHeight(WebViewActivity.this) - webView.getTop();
//            webView.setLayoutParams(params);
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

        }
    };

    /**
     * 返回键返回上一网页
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //多加一个判断 如果当前url和intent传进来的url是相等的 取反->走closeActivity()
        if (keyCode == KeyEvent.KEYCODE_BACK && bannerWeb.canGoBack()) {
            bannerWeb.goBack();
            return true;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }


}

